const { DB } = require('./sqlite3');

const PickDao = {
    addOne({ name, phone, department, ip }, callback) {
        let stmt = DB.prepare("INSERT INTO pick_log (name, phone, department, ip, c_time) VALUES ( ? , ? , ? , ? , ? )");
        stmt.run([name, phone, department, ip, Date.now()], (row, err) => {
            console.log('Add pick_log in DAO');
            if (err) {
                console.error('error in PickDAO.addOne', err);
                return typeof callback === "function" && callback({ err });
            } else {
                return typeof callback === "function" && callback({ row });
            }
        });
    },
    searchOne({ name, phone }, callback) {
        let stmt = DB.prepare("SELECT id,name,phone,department,c_time,ip FROM pick_log WHERE name = ? OR phone = ? ");
        stmt.get([name, phone], (err, row) => {
            console.log('search pick_log in DAO', row, err);
            if (err) {
                console.error('error in PickDao.searchOne', err);
                return typeof callback === "function" && callback({ err });
            } else {
                return typeof callback === "function" && callback({ row });
            }
        });
    },
    // getOne(id, callback) {
    //     DB.get("SELECT id,script,c_time,note FROM pick_log WHERE id = " + id, (err, row) => {
    //         if (err) return console.error(err);
    //         console.log('Get pick_log, id ', id);
    //         typeof callback === "function" && callback(row);
    //     });
    // },
    delOne(id, callback) {
        DB.run('DELETE FROM pick_log WHERE id = ' + id, (row, err) => {
            if (err) return console.error('error in PickDao.delOne', err);
            console.log('Delete pick_log, id ', id);
            typeof callback === "function" && callback(row);
        });
    },
    getList(callback) {
        DB.all("SELECT id,name,phone,department,c_time,ip FROM pick_log", (err, rows) => {
            console.log('List pick_log , length ', rows.length);
            if (err) return console.error('error in PickDao.getList', err);
            typeof callback === "function" && callback(rows);
        });
    },
    // addById({ id, script, note }, callback) {
    //     let stmt = DB.prepare("INSERT INTO pick_log (id, script, note , c_time) VALUES (? , ? , ? , ? )");
    //     stmt.run([id, script, note, Date.now()], (err) => {
    //         if (err) return console.error(err);
    //         console.log('Add pick_log', note);
    //         typeof callback === "function" && callback();
    //     });
    // },
}

module.exports.PickDao = PickDao;